from pydantic import BaseModel, EmailStr, ConfigDict
from typing import Optional, List
from datetime import datetime

class UserBase(BaseModel):
    username: str
    email: EmailStr
    is_active: bool = True
    is_superuser: bool = False

class UserCreate(UserBase):
    password: str

class UserUpdate(BaseModel):
    email: Optional[EmailStr] = None
    is_active: Optional[bool] = None

class User(UserBase):
    id: int
    created_at: datetime

    model_config = ConfigDict(from_attributes=True)

class ScriptBase(BaseModel):
    name: str
    description: Optional[str] = None
    content: str
    language: str = "python"  # 默认 Python
    is_active: bool = True

class ScriptCreate(ScriptBase):
    pass

class ScriptUpdate(BaseModel):
    name: Optional[str] = None
    description: Optional[str] = None
    content: Optional[str] = None
    language: Optional[str] = None
    is_active: Optional[bool] = None

class Script(ScriptBase):
    id: int
    owner_id: int
    created_at: datetime
    updated_at: datetime
    owner: Optional[User] = None

    model_config = ConfigDict(from_attributes=True)

class ExecutionLogBase(BaseModel):
    status: str = "pending"
    output: Optional[str] = None
    error_message: Optional[str] = None

class ExecutionLogCreate(ExecutionLogBase):
    script_id: int

class ExecutionLogUpdate(BaseModel):
    status: Optional[str] = None
    output: Optional[str] = None
    error_message: Optional[str] = None

class ExecutionLog(ExecutionLogBase):
    id: int
    script_id: int
    user_id: int
    executed_at: datetime
    completed_at: Optional[datetime]
    script: Optional[Script] = None
    user: Optional[User] = None

    model_config = ConfigDict(from_attributes=True)

class Token(BaseModel):
    access_token: str
    token_type: str

class TokenData(BaseModel):
    username: Optional[str] = None
